﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AutoMapper;
using Enferno.Public.InversionOfControl;
using Enferno.Public.Web.Mappers.Resolvers;
using Enferno.Public.Web.Models;
using Enferno.StormApiClient.Products;
using Enferno.Web.StormUtils;

namespace Enferno.Public.Web.Mappers.ProductProfiles
{
    public class ProductItemToVariantModelProfile :
        ProductItemToVariantBaseModelProfile
            <VariantModel, ProductFileModel, PriceModel, OnHandStatusModel, ParametricModel, ParametricValueModel>
    {
        
    }

    public class ProductItemToVariantBaseModelProfile<
        TVariantModel,
        TProductFileModel,
        TPriceModel,
        TOnHandStatusModel,
        TParametricModel,
        TParametricValueModel> : Profile
        where TVariantModel : VariantBaseModel<TOnHandStatusModel, TPriceModel, TProductFileModel, TParametricModel, TParametricValueModel>
        where TProductFileModel : ProductFileModel, new()
        where TPriceModel : PriceModel, new()
        where TOnHandStatusModel : OnHandStatusModel, new()
        where TParametricModel : ParametricBaseModel<TParametricValueModel>
        where TParametricValueModel : ParametricValueModel
    {
        protected override void Configure()
        {
            Mapper.CreateMap<ProductItem, TVariantModel>()
                .ForMember(to => to.ManufacturePartNo, opts => opts.MapFrom(from => from.Manufacturer.PartNo))
                .ForMember(to => to.Price, opts => opts.ResolveUsing<ProductItemPriceResolver<TPriceModel>>())
                .ForMember(to => to.Flags, opts => opts.ResolveUsing<ProductItemFlagsResolver>())
                .ForMember(to => to.Files, opts => opts.ResolveUsing<VariantItemFilesResolver<TProductFileModel>>())
                .ForMember(to => to.ImageKey, opts => opts.MapFrom(from => from.VariantImageKey.ToString()))
                .ForMember(to => to.ImageUrl,
                    opts =>
                        opts.ResolveUsing(
                            from => from.ImageKey.HasValue ? Link.ImageUrl(from.VariantImageKey.ToString()) : null))
                .ForMember(to => to.OnHandStatus, opts => opts.ResolveUsing<ProductItemOnHandStatusResolver<TOnHandStatusModel>>())
                .ForMember(to => to.VariantParametrics, opts => opts.Ignore()) //mapped in the builder
                .ForMember(to => to.Parametrics, opts => opts.Ignore());
        }
    }
}
